package com.qfedu.service;

import com.qfedu.dao.RankDao;
import com.qfedu.entity.Rank;
import com.qfedu.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RankServiceImpl implements RankService{

    @Autowired
    private RankDao rankDao;

    /**
     * 获取指定歌单的评分
     * @param songListId
     * @return
     */
    @Override
    public Integer getRankOfSongListId(int songListId) {
        return rankDao.getRankOfSongListId(songListId);
    }

    /**
     * 提交评分
     * @param rank
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 10,readOnly = false)
    @Override
    public ResponseData<String> setRank(Rank rank) {

        ResponseData<String> responseData = new ResponseData<>();

        //查询该用户是否已经评分
        Rank rank1 = rankDao.getRankBySongListIdAndUserId(rank);

        if (rank1 == null){

            //如果该评分已存在，则修改该用户对该歌单的评分
            int result = rankDao.setRank(rank);

            if (result == 1){

                responseData.setCode(1);

                responseData.setMsg("评分成功！");
            } else {

                responseData.setCode(0);

                responseData.setMsg("评分失败！");

            }

        } else {

            int result = rankDao.updateRank(rank);

            if (result == 1){

                responseData.setCode(1);

                responseData.setMsg("评分成功！");
            } else {

                responseData.setCode(0);

                responseData.setMsg("评分失败！");

            }
        }
        return responseData;
    }
}
